package com.easyedu.helper.service;

import com.alibaba.fastjson.JSON;
import com.easyedu.core.entity.po.Institute;
import com.easyedu.dean.entity.dto.request.ReqTeacher;
import com.easyedu.helper.constant.Name;
import com.easyedu.helper.dao.InstituteDao;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author mengranliu
 * @date 2022/3/8
 */
@Service
public class Teacher {

//     private final DeanClient deanClient;
//
//     public Teacher(DeanClient deanClient) {this.deanClient = deanClient;}

     @Autowired
     private InstituteDao instituteDao;

     public void createTeacher() throws IOException, InterruptedException {


          //获取所有专业信息
          HttpClient httpClient = HttpClient.newHttpClient();



          List<Institute> data = instituteDao.getInstitute();
          List<ReqTeacher> req = new ArrayList<>();
          //便利所有专业
          Random rand = new Random();
          Random rand1 = new Random();
          Random rand2 = new Random();
          for (Institute institute : data) {
               //生成10～20个老师
               int n = (int) (Math.random() * (20 - 10 )+10);
               for (int i = 0; i < n; i++) {
                    //生成姓
                    String firstName = Name.firstName[(rand.nextInt(Name.firstName.length))];
                    //生成名
                    String lastName = "";
                    for (int j = 0; j < rand2.nextInt(2)+1; j++) {
                         lastName = lastName + Name.lastName[rand1.nextInt(Name.lastName.length)];
                    }
                    String name = firstName + lastName;
                    ReqTeacher reqTeacher = new ReqTeacher();
                    reqTeacher.setTeacherNumber(institute.getDepartmentId() + i);
                    reqTeacher.setName(name);
                    reqTeacher.setInstituteId(institute.getInstituteId());
                    reqTeacher.setInstituteName(institute.getInstituteName());
                    reqTeacher.setDepartmentId(institute.getDepartmentId());
                    reqTeacher.setDepartmentName(institute.getDepartmentName());
                    req.add(reqTeacher);
               }
          }
          //生成老师
//          R teachers = deanClient.createTeachers(req);
//          System.out.println(teachers);
          String s = JSON.toJSONString(req);
          HttpRequest build = build = HttpRequest.newBuilder().uri(URI.create("http://127.0.0.1:8131/dean/teacher"))
                  .POST(BodyPublishers.ofString(s)).header("Content-Type", "application/json").build();
          HttpResponse<String> send = httpClient.send(build, BodyHandlers.ofString());
          System.out.println(send.body());

     }


}
